package com.administrators.bag;

import java.sql.SQLException;
import java.util.ArrayList;

import com.Wzs.lbtcp.lbtcp;
import com.wzsck.util.DBManager;
import com.wzsck.util.QueryDataResult;

public class AdministratorsManager {
	//查询query
			public ArrayList<Administrators> query(){//把东西存到动态数组里面
				ArrayList<Administrators> list = new ArrayList<Administrators>();
				String sql = "select * from wzs_administrators";//sql语句查表book里的东西
				QueryDataResult qdr =DBManager.executeQuery(sql);
				if (qdr.rs!=null){
					try {
						while (qdr.rs.next()){
							String id=qdr.rs.getString("id");
							String name=qdr.rs.getString("name");//书名							
							String password=qdr.rs.getString("password");//价格
							Administrators x= new Administrators(id,name,password);
							list.add(x);
						}
					} catch (SQLException e) {
						// TODO Auto-generated catch block
						e.printStackTrace();
					}
				}
				return list;
			}
			//add
			public int add( String name,String mima) {
				String sql = String.format(
						"insert into wzs_administrators(`name`,`password`) values('%s','%s')",name, mima);
				
				System.out.print(sql);
				int i = DBManager.executeUpdate(sql);
				return i;

			}
			public int update(int id, String name,String mima) {
				String sql = String.format(
						"update wzs_administrators `name`='%s',`password`='%s' , where id = %d",
						 name,mima,id);
				int i = DBManager.executeUpdate(sql);
				return i;
			}
			public int del(int id) {
				String sql = "delete from wzs_administrators where id = " + id;
				int i = DBManager.executeUpdate(sql);
				return i;
			}
			//==========================================================
			 public int count() {//count查非空
			 		String sql="select count(*) from wzs_administrators";//换表名
			 		QueryDataResult qdr=DBManager.executeQuery(sql);
			 		if (qdr.rs!=null) {
			 			try {
			 				while (qdr.rs.next()) {
			 					int count =qdr.rs.getInt("count(*)");
			 					return count;
			 				}
			 			} catch (SQLException e) {
			 				// TODO Auto-generated catch block
			 				e.printStackTrace();
			 			}
			 		}
			 		return 0;
			 	}
			 
			     public ArrayList<Administrators> chaxun(int page,int limit) {
			 	    ArrayList<Administrators> list =new ArrayList<Administrators>();//limit返回行数 limit 开始数 , 行数
						String sql="select * from  wzs_administrators  limit "+(page - 1)*limit+","+limit;//换表名
						QueryDataResult qdr=DBManager.executeQuery(sql);
						Administrators x=null;
						if (qdr.rs!=null){
							try {
								while (qdr.rs.next()){
									String id=qdr.rs.getString("id");
									String name=qdr.rs.getString("name");//书名
									
									String password=qdr.rs.getString("password");//价格
									x= new Administrators(id,name,password);
									list.add(x);
								}
							} catch (SQLException e) {
								// TODO Auto-generated catch block
								e.printStackTrace();
							}
						}
						qdr.close();
						return list;		
				}  
			    
}


				 
			    
